package com.beiing.tianshuai.tianshuai.mine.ui;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.IdentificationStatusBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.util.List;

import butterknife.BindView;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class IdentificationActivity extends BaseActivity {

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.rl_personal_identification)
    RelativeLayout mTvPersonalIdentification;
    @BindView(R.id.rl_club_identification)
    RelativeLayout mTvClubIdentification;
    @BindView(R.id.rl_company_identification)
    RelativeLayout mTvCompanyIdentification;
    @BindView(R.id.tv_personal_identification_status)
    TextView mTvPersonalIdentificationStatus;
    @BindView(R.id.tv_club_identification_status)
    TextView mTvClubIdentificationStatus;
    @BindView(R.id.tv_company_identification_status)
    TextView mTvCompanyIdentificationStatus;

    private Context mContext;
    private List<IdentificationStatusBean.DataBean> mDataBeanList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_identification;
    }

    @Override
    protected void init() {
        mContext = this;
        initToolbar();
        getIdentificationStatus(UserInfoBean.getUid(mContext));
        initListener();
    }

    private void initToolbar() {
        mToolbarIvBack.setVisibility(View.VISIBLE);
        mToolbarTvTitle.setText("资质认证");
    }

    private void initListener() {
        // 企业认证
        mTvCompanyIdentification.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!mDataBeanList.isEmpty()) {
                    String status = mDataBeanList.get(2).getStatus();
                    if (!TextUtils.isEmpty(status)) {
                        switch (status) {
                            case "1":
                                Intent identificationInfoIntent = new Intent(mContext, IdentificationInfoActivity.class);
                                identificationInfoIntent.putExtra("type", "4");
                                startActivity(identificationInfoIntent);
                                break;
                            case "0":
                                Intent intent = new Intent(mContext, IdentificationStatusActivity.class);
                                intent.putExtra("status", "1");
                                startActivity(intent);
                                break;
                            case "2":
                                Intent intentFailed = new Intent(mContext, IdentificationStatusActivity.class);
                                intentFailed.putExtra("status", "5");
                                startActivity(intentFailed);
                                break;
                        }
                    } else {
                        Intent intent = new Intent(mContext, CompanyIdentificationActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });

        // 社团认证
        mTvClubIdentification.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!mDataBeanList.isEmpty()) {
                    String status = mDataBeanList.get(1).getStatus();
                    if (!TextUtils.isEmpty(status)) {
                        switch (status) {
                            case "1":
                                Intent identificationInfoIntent = new Intent(mContext, IdentificationInfoActivity.class);
                                identificationInfoIntent.putExtra("type", "3");
                                startActivity(identificationInfoIntent);
                                break;
                            case "0":
                                Intent intent = new Intent(mContext, IdentificationStatusActivity.class);
                                intent.putExtra("status", "1");
                                startActivity(intent);
                                break;
                            case "2":
                                Intent intentFailed = new Intent(mContext, IdentificationStatusActivity.class);
                                intentFailed.putExtra("status", "5");
                                startActivity(intentFailed);
                                break;
                        }
                    } else {
                        Intent intent = new Intent(mContext, ClubIdentificationActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });

        // 个人认证
        mTvPersonalIdentification.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!mDataBeanList.isEmpty()) {
                    String status = mDataBeanList.get(0).getStatus();
                    if (!TextUtils.isEmpty(status)) {
                        switch (status) {
                            case "1":
                                Intent identificationInfoIntent = new Intent(mContext, IdentificationInfoActivity.class);
                                identificationInfoIntent.putExtra("type", "2");
                                startActivity(identificationInfoIntent);
                                break;
                            case "0":
                                Intent intent = new Intent(mContext, IdentificationStatusActivity.class);
                                intent.putExtra("status", "1");
                                startActivity(intent);
                                break;
                            case "2":
                                Intent intentFailed = new Intent(mContext, IdentificationStatusActivity.class);
                                intentFailed.putExtra("status", "5");
                                startActivity(intentFailed);
                                break;
                        }
                    } else {
                        Intent intent = new Intent(mContext, PersonalIdentificationActivity.class);
                        startActivity(intent);
                    }
                }
            }
        });

        // 返回
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void getIdentificationStatus(String uid) {
        DisposableObserver<IdentificationStatusBean> observer = new DisposableObserver<IdentificationStatusBean>() {
            @Override
            public void onNext(@NonNull IdentificationStatusBean identificationStatusBean) {
                mDataBeanList = identificationStatusBean.getData();
                for (IdentificationStatusBean.DataBean bean : mDataBeanList) {
                    String type = bean.getType();
                    String status = bean.getStatus();
                    if (TextUtils.equals(type, "personal")) {
                        if (!TextUtils.isEmpty(status)) {
                            setIdentificationStatus(mTvPersonalIdentificationStatus, status);
                        }
                    } else if (TextUtils.equals(type, "corporation")) {
                        if (!TextUtils.isEmpty(status)) {
                            setIdentificationStatus(mTvClubIdentificationStatus, status);
                        }
                    } else if (TextUtils.equals(bean.getType(), "company")) {
                        if (!TextUtils.isEmpty(status)) {
                            setIdentificationStatus(mTvCompanyIdentificationStatus, status);
                        }
                    }
                }
            }

            @Override
            public void onError(@NonNull Throwable e) {
                showShortText("请求失败");
                LogUtils.e(e.getMessage(), IdentificationActivity.class.getName());
            }

            @Override
            public void onComplete() {

            }
        };

        DataManager.getIdentificationStatus(uid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    private void setIdentificationStatus(TextView view, String status) {
        switch (status) {
            case "0":
                view.setText("审核中");
                break;
            case "1":
                view.setText("已认证");
                break;
            case "2":
                view.setText("未通过");
                break;
            default:
                view.setText("");
        }
    }
}
